<template>
  <a-input-search
    :value="fakeValue"
    v-bind="$attrs"
    @change="onChange"
    @search="
      () => {
        visible = true
      }
    "
  >
    <template #prefix>
      <s-icon v-if="!!fakeValue" :type="fakeValue" />
    </template>
    <template #enterButton>
      <a-popover v-model:visible="visible" trigger="click">
        <template #content>
          <a-tabs tab-position="left" :style="{ height: '400px', width: '650px' }" size="small">
            <a-tab-pane v-for="(one, index) in icons" :key="index + 'left-one'" :tab="one.title">
              <a-tabs
                v-if="one.children && one.children.length > 0"
                tab-position="top"
                :style="{ height: '400px', width: '500px' }"
                size="small"
              >
                <a-tab-pane
                  v-for="(two, index) in one.children"
                  :key="index + 'two'"
                  :tab="two.title"
                >
                  <s-scroll :style="{ height: '330px' }">
                    <a-row align="middle">
                      <template v-for="(icon, index) in two.icons" :key="index + 'icon'">
                        <a-col class="icon-select-content" :span="8" @click="iconCheck(icon)">
                          <div class="icon-select-icon">
                            <s-icon :type="icon" />
                          </div>
                          <div class="icon-select-title">
                            <span>{{ icon }}</span>
                          </div>
                        </a-col>
                      </template>
                    </a-row>
                  </s-scroll>
                </a-tab-pane>
              </a-tabs>
              <template v-else>
                <s-scroll :style="{ height: '400px' }">
                  <a-row align="middle">
                    <template v-for="(icon, index) in one.icons" :key="index + 'icon'">
                      <a-col class="icon-select-content" :span="8" @click="iconCheck(icon)">
                        <div class="icon-select-icon">
                          <s-icon :type="icon" />
                        </div>
                        <div class="icon-select-title">
                          <span>{{ icon }}</span>
                        </div>
                      </a-col>
                    </template>
                  </a-row>
                </s-scroll>
              </template>
            </a-tab-pane>
          </a-tabs>
        </template>
        <s-icon type="search-outlined" />
      </a-popover>
    </template>
  </a-input-search>
</template>
<script>
import icons from './icons.js'
export default {
  // 继承
  name: 'SIconSelect',
  props: {
    modelValue: {
      required: false
    }
  },
  emits: ['change', 'update:modelValue'],
  data() {
    return {
      visible: false,
      fakeValue: this.modelValue,
      icons: icons
    }
  },
  watch: {
    modelValue(val) {
      if (val !== this.fakeValue) {
        this.fakeValue = val
      }
    }
  },
  methods: {
    iconCheck(icon) {
      this.visible = false
      this.$emit('update:modelValue', icon)
      this.$emit('change', icon)
      this.fakeValue = icon
    },
    onChange(e) {
      this.$emit('update:modelValue', e.target.value)
      this.$emit('change', e.target.value)
    }
  }
}
</script>
<style scoped type="less">
.icon-select-content {
  cursor: pointer;
  padding: 5px 10px 5px 10px;
  border-radius: 5px;
}
.icon-select-content:hover {
  background: #1890ff;
  color: white;
}
.icon-select-title {
  width: 100%;
  text-align: center;
}
.icon-select-title > span {
  font-size: 10px;
}
.icon-select-icon {
  width: 100%;
  text-align: center;
  font-size: 25px;
}
</style>
